﻿using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.AsmType;
using System;


namespace AgencyManagerSystem.Core
{
    public class DocTypeUpdaterCreator : BaseUpdaterCreator<DocTypeModel>
    {
        public DocTypeUpdater CreateDocTypeUpdaterInstance()
        {
            DocTypeUpdater docTypeUpdater = new DocTypeUpdater(new DocTypeFetcher());
            docTypeUpdater.DataUpdating += FlagAndApproveFlagValid;
            docTypeUpdater.DataUpdating += CodeUnique;
            docTypeUpdater.DataUpdating += DataNotSpace;
            return docTypeUpdater;
        }

        private void DataNotSpace(BaseUpdater<DocTypeModel> sender, DataUpdatingEventArgs<DocTypeModel> e)
        {
            if (e.Insert == null) return;
            if(String.IsNullOrWhiteSpace(e.Insert.Code)||String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Cancel = true;
                e.Message = "编号或名称不能为空";
            }
        }

        private void CodeUnique(BaseUpdater<DocTypeModel> sender, DataUpdatingEventArgs<DocTypeModel> e)
        {
            if (e.Insert == null) return;
            DocTypeFetcher docTypeFetcher = new DocTypeFetcher();
            var dt = docTypeFetcher.GetByCode(e.Insert.Code);
            if(dt!=null&&dt.AutoId!=e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = "编号已存在";
            }

        }
    }

}
